<template>
  <div class="monthlyMeeting-container page-common-layout">
    <div class="screen-conditions-container">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <a-month-picker
            v-model="condition.monthData"
            placeholder="请选择月份"
            format="YYYY-MM"
            valueFormat="YYYY-MM"
          />
        </template>
        <template slot="operate">
          <a-button class="cu-btn-primary" icon="search" @click="search()">
            查询
          </a-button>
          <a-button class="cu-btn-filter" icon="reload" @click="reset()">
            重置
          </a-button>
        </template>
      </ScreenConditions>
    </div>
    <div class="meeting-body-container">
      <div class="operate">
        <a-button
          @click="handleAdd({}, 1)"
          class="cu-btn-primary margin-bottom-xs"
          icon="plus"
          v-action:lhhz_add
        >
          新增
        </a-button>
      </div>
      <div class="main">
        <vxe-table
          ref="xTable"
          height="99%"
          align="center"
          show-overflow
          show-header-overflow
          border="inner"
          :data="tableData"
          :column-config="{ resizable: true }"
          size="small"
          :loading="loading"
        >
          <vxe-column type="seq" width="80" title="序号"></vxe-column>
          <vxe-column field="monthData" title="例会月份"> </vxe-column>
          <vxe-column field="completeCount" title="已填报项目数(个)">
          </vxe-column>
          <vxe-column field="laggingCount" title="滞后项目数(个)"> </vxe-column>
          <vxe-column title="月度例会">
            <template #default="{ row }">
              <div class="operate-icon">
                <a-tooltip>
                  <template slot="title">
                    预览
                  </template>
                  <a-icon type="eye" @click="handeViewFile(row)" />
                </a-tooltip>
                <a-tooltip>
                  <template slot="title">
                    下载
                  </template>
                  <a-icon type="download" @click="handelDownloadFile(row)" />
                </a-tooltip>
              </div>
            </template>
          </vxe-column>
          <vxe-column
            field="operate"
            width="160"
            title="操作"
            fixed="right"
            align="left"
          >
            <template #default="{ row }">
              <div class="text-button-group">
                <a-tooltip v-action:lhhz_edit>
                  <template slot="title">
                    例会文件管理
                  </template>
                  <a-icon
                    type="file"
                    class="file-icon"
                    @click="handleFiles(row)"
                  />
                </a-tooltip>
                <a-tooltip>
                  <template slot="title">
                    查看
                  </template>
                  <a-icon type="file-search" @click="handleAdd(row, 3)" />
                </a-tooltip>
                <a-tooltip v-action:lhhz_edit>
                  <template slot="title">
                    编辑
                  </template>
                  <a-icon type="edit" @click="handleAdd(row, 2)" />
                </a-tooltip>
                <a-tooltip v-action:lhhz_delete>
                  <template slot="title">
                    删除
                  </template>
                  <a-icon type="delete" @click="handleDelClick(row)" />
                </a-tooltip>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>

      <div class="pager-container">
        <vxe-pager
          size="small"
          background
          :current-page.sync="pager.page"
          :page-size.sync="pager.pageSize"
          :total="pager.total"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes']"
          :page-sizes="[20, 50, 100, 500]"
          @page-change="paginationChange"
        >
          <template #left>
            <div class="vxe-pager-left-tip-container">
              共<span>{{ pager.total }}</span
              >条
            </div>
          </template>
        </vxe-pager>
      </div>
    </div>
    <!-- 新增 -->
    <addPage
      :key="addPage.key"
      :visible.sync="addPage.visible"
      :parameter="addPage.soucre"
      :operateType="addPage.operateType"
    ></addPage>
    <!-- 例会文件管理 -->
    <filesPage
      :key="filesPage.key"
      :visible.sync="filesPage.visible"
      :parameter="filesPage.soucre"
    ></filesPage>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { downloadDfs } from "@/utils/TopVUI";
import { baseUrl } from "@/services/baseUrl";
import ScreenConditions from "@/components/extendCustom/ScreenConditions";
// 新增
import addPage from "./components/addPage.vue";
//例会文件管理
import filesPage from "./components/filesPage.vue";
import { pageMonthlyMeeting, deleteDataByMonth } from "@/api/safetyManage";
export default {
  components: {
    ScreenConditions,
    addPage,
    filesPage,
  },
  data() {
    return {
      condition: {
        monthData: "",
      },
      //列表
      loading: false,
      tableData: [],
      pager: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      //新增
      addPage: {
        key: "add_0",
        visible: false,
        operateType: 1,
        soucre: {},
      },
      //例会文件管理
      filesPage: {
        key: "files_0",
        visible: false,
        soucre: {},
      },
    };
  },
  computed: {
    ...mapGetters(["configData"]),
    kkFileView() {
      let index = this.configData.findIndex(
        (item) => item.code === "kkFileView"
      );
      if (index >= 0) {
        return this.configData[index].value;
      } else {
        return "http://192.168.1.216:8012/onlinePreview";
      }
    },
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    //预览
    handeViewFile(row) {
      let { monthData } = row;
      let path =
        baseUrl +
        `/probusiness/monthlyMeetings/shop/exportExcel?monthData=${monthData}&fullfilename=${Date.now()}.xls`;
      let url =
        this.kkFileView +
        "/onlinePreview?url=" +
        encodeURIComponent(Base64.encode(path));
      window.open(url);
    },

    //下载
    handelDownloadFile(row) {
      let { monthData } = row;
      let path =
        baseUrl +
        `/probusiness/monthlyMeetings/shop/exportExcel?monthData=${monthData}`;
      downloadDfs(path);
    },
    // 例会文件管理
    handleFiles(row) {
      this.filesPage.key = Date.now();
      this.filesPage.soucre = row;
      this.filesPage.visible = true;
    },
    // 新增
    handleAdd(row, type) {
      this.addPage.key = Date.now();
      this.addPage.soucre = row;
      this.addPage.operateType = type;
      this.addPage.visible = true;
    },
    //删除
    handleDelClick(row) {
      let dataLength = this.tableData.length;
      let that = this;
      that.$confirm({
        title: "提示",
        content: "确定删除该数据吗？删除之后不可恢复！",
        okText: "确定",
        cancelText: "取消",
        icon: () =>
          that.$createElement("a-icon", {
            props: {
              key: "warning",
              type: "exclamation-circle",
              twoToneColor: "#faad14",
            },
          }),
        onOk() {
          deleteDataByMonth({ monthData: row.monthData })
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                that.$message.success("删除成功", 1.5);
                if (dataLength == 1 && that.pager.page > 1) {
                  that.pager.page -= 1;
                }
                that.getTableData();
              } else {
                that.$message.error(res.message);
              }
            })
            .catch((_) => {});
        },
      });
    },
    search() {
      this.pager.page = 1;
      this.getTableData();
    },
    reset() {
      this.condition.monthData = "";
      this.search();
    },
    //查询列表数据
    getTableData() {
      this.loading = true;
      let { page, pageSize } = this.pager;
      let { monthData } = this.condition;
      pageMonthlyMeeting({
        page,
        pageSize,
        monthData,
      })
        .then((res) => {
          this.tableData = res?.data.rows || [];
          this.pager.total = res?.data.totalCount || 0;
        })
        .catch((_) => {
          this.tableData = [];
          this.pager.total = 0;
        })
        .finally((_) => {
          this.loading = false;
        });
    },
    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },
  },
};
</script>

<style lang="less" scoped>
.monthlyMeeting-container {
  display: flex;
  flex-direction: column;
  .meeting-body-container {
    flex: 1;
    background-color: @base-bg-color;
    flex-shrink: 0;
    position: relative;
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    .operate {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .main {
      flex: 1;
      .operate-icon {
        .anticon {
          cursor: pointer;
          font-size: 18px;
          color: #1677ff;
        }
        .anticon-eye {
          margin-right: 20px;
        }
        .anticon-download {
          color: #239b53;
        }
      }
      .text-button-group {
        .file-icon {
          color: #c652f0;
        }
      }
    }
    .pager-container {
      flex-shrink: 0;
      box-sizing: border-box;
    }
  }
}
</style>
